package com.example.shopping1902.cart;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.shopping1902.R;
import com.example.shopping1902.me.UserBean;
import com.example.shopping1902.utils.BaseActivity;
import com.example.shopping1902.utils.SaveUserUtils;
import com.example.shopping1902.utils.URLUtils;
import com.google.gson.Gson;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class CartActivity extends BaseActivity {
    ListView lv;
    TextView moneyTv;
    List<CartBean.DataBean> data;//数据源
    private CartAdapter adapter;
    List<Boolean>statuList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        lv = findViewById(R.id.cart_daily_lv);
        moneyTv = findViewById(R.id.cart_cal_tv);
        data = new ArrayList<>();//适配器
        statuList = new ArrayList<>();
        adapter = new CartAdapter(this,data,statuList);
        lv.setAdapter(adapter);

        //加载数据
        UserBean.DataBean userinfo = getUserinfo();
        int id = userinfo.getId();
        String url = URLUtils.queryCartURL+"?userId="+id;
        connectNetWork(url);
    }

    @Override
    public void onSuccess(String result) {
        CartBean cartBean = new Gson().fromJson(result, CartBean.class);
        List<CartBean.DataBean> list = cartBean.getData();
        for (int i = 0;i<list.size();i++){
            statuList.add(false);
        }
        if (list!=null&&list.size()>0) {
            data.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    //获取用户信息的方法
    private UserBean.DataBean getUserinfo(){
        UserBean.DataBean bean = null;
        bean = SaveUserUtils.getUserinfo();
        if (bean == null) {
            bean = SaveUserUtils.getUserToObj(this);
        }
        return bean;
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.all_daily_back:
                finish();
                break;
            case R.id.cart_calculate_btn:
                break;
        }
    }
}
